package listbyorder.access001_100.test94;

import listbyorder.utils.TreeNode;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/6/6 9:16
 */
public class Solution2 {

    // 方法二： 迭代方式下的中序遍历
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        if (root != null) {
            Stack<TreeNode> stack = new Stack<>();
            while (!stack.isEmpty() || root != null) {
                if(root != null){
                    stack.push(root);
                    root = root.left;
                }else {
                    root = stack.pop();
                    ans.add(root.val);
                    root = root.right;
                }
            }
        }
        return ans;
    }
}
